package com.findroader.blog.page;


import java.io.Serializable;
import java.util.List;

public class Page<T> implements Serializable {

	public Page(int pageNo, int pageSize, long totalRecords) {

		this.pageNo = pageNo;
		this.pageSize = pageSize;
		this.totalRecords = totalRecords;
		this.totalPages = this.getTotalPages();
		this.hasNext = this.isHasNext();
		this.hasPre = this.isHasPre();
		this.firstResult = this.getFirstResult();

	}

	public Page(int pageSize) {
		this.pageSize = pageSize;
	}
	public Page() {

	}

	private int pageNo = 1;

	private int pageSize;

	private boolean hasPre;

	private boolean hasNext;

	private int firstResult;

	private int totalPages;

	private long totalRecords;

	private List<T> result;

	boolean autoCount = true;


	public int getPageNo() {
        
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		if (pageNo <= 0)
			pageNo = 1;
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public boolean isHasPre() {

		int pageNo = this.getPageNo();
		hasPre = pageNo > 1 && this.getTotalPages() > 1;
		return hasPre;

	}

	public void setHasPre(boolean hasPre) {
		this.hasPre = hasPre;
	}

	public boolean isHasNext() {
		this.hasNext = this.getPageNo() >= 1
				&& this.getTotalPages() > this.getPageNo();
		return hasNext;
	}

	public void setHasNext(boolean hasNext) {
		this.hasNext = hasNext;
	}

	public int getFirstResult() {

		this.firstResult = (this.pageNo - 1) * pageSize + 1;
		return firstResult;
	}

	public void setFirstResult(int firstResult) {
		this.firstResult = firstResult;
	}

	public int getTotalPages() {
		if (totalRecords == 0) {
			this.totalPages = 0;
			return 0;
		}
		totalPages = (int) (totalRecords - 1) / pageSize + 1;
		return totalPages;
	}

	public void setTotalPages(int totalPages) {
		this.totalPages = totalPages;
	}

	public long getTotalRecords() {
		return totalRecords;
	}

	public void setTotalRecords(long totalRecords) {
		this.totalRecords = totalRecords;
	}

	public List<T> getResult() {
		return result;
	}

	public void setResult(List<T> result) {
		this.result = result;
	}

	public boolean isAutoCount() {
		return autoCount;
	}

	public void setAutoCount(boolean autoCount) {
		this.autoCount = autoCount;
	}

}
